﻿<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet  [
    <!ENTITY nbsp   "&#160;">
    <!ENTITY copy   "&#169;">
    <!ENTITY reg    "&#174;">
    <!ENTITY trade  "&#8482;">
    <!ENTITY mdash  "&#8212;">
    <!ENTITY ldquo  "&#8220;">
    <!ENTITY rdquo  "&#8221;">
    <!ENTITY pound  "&#163;">
    <!ENTITY yen    "&#165;">
    <!ENTITY euro   "&#8364;">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="mainframe.xsl"/>
    <xsl:param name="setting"/>
    <xsl:param name="maxcolid"/>
    <xsl:param name="type" />
    
    <xsl:template name="main">

        <form method="post" name="form1" onsubmit="return checkForm(this)">
            <input type="hidden" name="app">
                <xsl:attribute name="value">
                    <xsl:choose>
                        <xsl:when test="/root/app/@name!=''">
                            <xsl:value-of select="/root/app/@name"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="/root/system/url/@app"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </input>
            <input type="hidden" name="entity">
                <xsl:attribute name="value">
                    <xsl:choose>
                        <xsl:when test="/root/app/entity/@name!=''">
                            <xsl:value-of select="/root/app/entity/@name"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="/root/system/url/@entity"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </input>
            <h1>
                <xsl:choose>
                    <xsl:when test="/root/system/url/@action='buildcategory'">
                        Build Category
                    </xsl:when>
                    <xsl:otherwise>
                        Build Entity
                    </xsl:otherwise>
                </xsl:choose>
            </h1>
            <table border="1" cellpadding="4" cellspacing="0">
                <tr>
                    <th>Name(en-us)</th>
                    <td>
                        <input type="text" name="name" value="{/root/app/entity/@title}" />
                    </td>
                </tr>
                <tr>
                    <th>Name(zh-cn)</th>
                    <td>
                        <input type="text" name="namezh-cn" value="{/root/app/entity/@zh-cn}" />
                    </td>
                </tr>
                <tr>
                    <th>Description</th>
                    <td>
                        <textarea name="description">
                            <xsl:value-of select="/root/app/entity/description"/>
                        </textarea>
                    </td>
                </tr>
                <tr>
                    <th>&nbsp;</th>
                    <td>
                        <input type="submit" value="Submit" />
                    </td>
                </tr>
            </table>

            <br/>
            <xsl:choose>
            <xsl:when test="/root/system/url/@action='buildentity' or /root/system/url/@status='1'">
                <a href="?app={/root/system/url/@app}&amp;entity={/root/system/url/@entity}&amp;action=buildcategory">修改分类表&gt;&gt;</a>
            </xsl:when>
                <xsl:otherwise>
                    <a href="?app={/root/system/url/@app}&amp;entity={/root/system/url/@entity}&amp;action=buildentity&amp;status=1">修改数据表&gt;&gt;</a>    
                </xsl:otherwise>
            </xsl:choose>

            <table border="1"  bordercolor="#000000" cellpadding="4" cellspacing="0" rules="rows" id="tablecolumns">
                <thead>
                    <tr>
                        <th colspan="5" class="trYline">
                            数据结构
                        </th>
                        <th colspan="3" class="trYline">
                            界面
                        </th>
                        <xsl:if test="$type='entity'">
                        <th colspan="4" class="trYline">                            
                            显示页
                        </th>
                        </xsl:if>
                        <th colspan="2">其他</th>
                    </tr>
                    <tr>
                        <th>
                            字段名称
                        </th>
                        <th>
                            字段类型
                        </th>
                        <th>
                            长度
                        </th>
                        <th>
                            允许空
                        </th>
                        <th class="trYline">
                            默认值
                        </th>
                        <th>
                            控件
                        </th>
                        <th>
                            名称 zh-cn
                        </th>
                        <th class="trYline">
                            名称 en-us
                        </th>
                        <xsl:if test="$type='entity'">
                        <th>
                            列表
                        </th>
                        <th>
                            添加
                        </th>
                        <th>
                            修改
                        </th>
                        <th class="trYline">
                            查看
                        </th>
                        </xsl:if>
                        <th>
                            排序
                        </th>
                        <th>
                            删除
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:choose>
                        <xsl:when test="/root/app/entity/columns/column">
                            <!--修改Entity开始-->
                            <xsl:call-template name="ColumnList">
                                <xsl:with-param name="column" select="/root/app/entity/columns/column" />
                                <xsl:with-param name="pages" select="/root/app/entity/pages" />
                            </xsl:call-template>
                            <!--修改Entity结束-->
                        </xsl:when>
                        <xsl:otherwise>
                            <!--新增Entity开始-->
                            <xsl:call-template name="ColumnList">
                                <xsl:with-param name="column" select="$setting/root/syscol/column" />
                                <xsl:with-param name="pages" select="$setting/root/pages" />
                            </xsl:call-template>
                            <!--新增Entity结束-->
                        </xsl:otherwise>
                    </xsl:choose>
                </tbody>
            </table>
            <div id="sortPannel">
                <h5>
                    <a href="javascript:void(0)" onclick="$('#sortPannel').fadeOut('fast');$('#tablecolumns tbody tr.cur').removeClass('cur')">[x]</a>字段排序
                </h5>
                <button type="button" onclick="moveUp()">Move Up</button>
                <button type="button" onclick="moveDown()">Move Down</button>                
            </div>
            <a href="javascript:void(0)" onclick="addColumns(5)">+添加字段</a>
            <input type="hidden" id="maxcolid" value="{$maxcolid}" />
            <br/>
            <br/>
            <br/>
            <br/>
            <br/>
        </form>

        <script type="text/javascript">
            <xsl:comment>
                $(document).ready(function(){addColumns(5);
                $("#tablecolumns tbody tr").bind("mouseover", function(){$(this).addClass('h')}).bind("mouseout",function(){$(this).removeClass('h')});
                });
            </xsl:comment>
        </script>
    </xsl:template>
    
    <xsl:template name="ColumnList">
        <xsl:param name="column"/>
        <xsl:param name="pages"/>
        <xsl:for-each select="$column">
            <tr class="tr_{@name}">
                <!--<xsl:if test="@changedefault='0'">
                                    <xsl:attribute name="class">
                                        <xsl:value-of select="'readonly'"/>
                                    </xsl:attribute>
                                </xsl:if>-->
                <td>
                    <input type="hidden" name="colid" value="{@colid}" class="txt" />
                    <input type="text" name="colname" value="{@name}" class="txt" />
                </td>
                <td>
                    <xsl:call-template name="DataType">
                        <xsl:with-param name="name" select="'coltype'"/>
                        <xsl:with-param name="type" select="@type"/>
                    </xsl:call-template>
                </td>
                <td>
                    <input type="text" name="colsize" value="{@size}" class="txt2"/>
                </td>
                <td>
                    <input type="hidden" name="colbenull">
                        <xsl:attribute name="value">
                            <xsl:choose>
                                <xsl:when test="@null='1'">
                                    <xsl:value-of select="1"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="0"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                    </input>
                    <input type="checkbox" value="1" class="chk" onclick="$(this).prev('input').val(this.checked?1:0)">
                        <xsl:if test="@null='1'">
                            <xsl:attribute name="checked">
                                <xsl:value-of select="'checked'"/>
                            </xsl:attribute>
                        </xsl:if>
                    </input>
                </td>
                <td class="trYline">
                    <input type="text" name="coldefault" value="{@default}" class="txt" />
                </td>
                <td>
                    <xsl:call-template name="Controls">
                        <xsl:with-param name="name" select="'colcontrol'"/>
                        <xsl:with-param name="ctrl" select="@control"/>
                    </xsl:call-template>
                </td>
                <td>
                    <input type="text" name="colnamezhcn" value="{@zh-cn}" class="txt"/>
                </td>
                <td class="trYline">
                    <input type="text" name="colnameenus" value="{@en-us}" class="txt" />
                </td>

                <xsl:if test="$type='entity'">
                <td>
                    <xsl:variable name="name" select="@name"/>
                    <input type="hidden" name="collist">
                        <xsl:attribute name="value">
                            <xsl:choose>
                                <xsl:when test="$pages/page[@name='list']/column[@name=$name]">
                                    <xsl:value-of select="1"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="0"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                    </input>
                    <input type="checkbox" class="chk" value="1" onclick="$(this).prev('input').val(this.checked?1:0)">                        
                        <xsl:if test="$pages/page[@name='list']/column[@name=$name]">
                            <xsl:attribute name="checked">checked</xsl:attribute>
                        </xsl:if>
                    </input>
                </td>
                <td>
                    <xsl:variable name="name" select="@name"/>
                    <input type="hidden" name="coladd">
                        <xsl:attribute name="value">
                            <xsl:choose>
                                <xsl:when test="$pages/page[@name='add']/column[@name=$name]">
                                    <xsl:value-of select="1"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="0"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                    </input>
                    <input type="checkbox" class="chk" value="1" onclick="$(this).prev('input').val(this.checked?1:0)">
                        <xsl:if test="$pages/page[@name='add']/column[@name=$name]">
                            <xsl:attribute name="checked">checked</xsl:attribute>
                        </xsl:if>
                    </input>                    
                </td>
                <td>
                    <xsl:variable name="name" select="@name"/>
                    <input type="hidden" name="colmodify">
                        <xsl:attribute name="value">
                            <xsl:choose>
                                <xsl:when test="$pages/page[@name='modify']/column[@name=$name]">
                                    <xsl:value-of select="1"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="0"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                    </input>
                    <input type="checkbox" class="chk" value="1" onclick="$(this).prev('input').val(this.checked?1:0)">
                        <xsl:if test="$pages/page[@name='modify']/column[@name=$name]">
                            <xsl:attribute name="checked">checked</xsl:attribute>
                        </xsl:if>
                    </input>                 
                </td>
                <td class="trYline">
                    <xsl:variable name="name" select="@name"/>
                    <input type="hidden" name="colview">
                        <xsl:attribute name="value">
                            <xsl:choose>
                                <xsl:when test="$pages/page[@name='view']/column[@name=$name]">
                                    <xsl:value-of select="1"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="0"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute>
                    </input>
                    <input type="checkbox" class="chk" value="1" onclick="$(this).prev('input').val(this.checked?1:0)">
                        <xsl:if test="$pages/page[@name='view']/column[@name=$name]">
                            <xsl:attribute name="checked">checked</xsl:attribute>
                        </xsl:if>
                    </input>                    
                </td>
                </xsl:if>
                <td>
                    <a href="javascript:void(0)" onclick="sortColumn(this)">排序</a>
                </td>
                <td>
                    <a href="javascript:void(0)" onclick="removeColumn(this)">删除</a>
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="DataType">
        <xsl:param name="name"/>
        <xsl:param name="type"/>
        <select name="{$name}">
            <xsl:for-each select="$setting/root/datatype/type">
                <option value="{@name}">
                    <xsl:if test="$type=@name">
                        <xsl:attribute name="selected">
                            <xsl:value-of select="'selected'"/>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="@name"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>

    <xsl:template name="Controls">
        <xsl:param name="name"/>
        <xsl:param name="ctrl"/>
        <select name="{$name}">
            <xsl:for-each select="$setting/root/control/item">
                <option value="{.}">
                    <xsl:if test="$ctrl=.">
                        <xsl:attribute name="selected">
                            <xsl:value-of select="'selected'"/>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="."/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>

    <xsl:template name="css">
        <style type="text/css">
            #tablecolumns { margin: 1em 0;}
            
            #tablecolumns a {color:#0044c1}
            #tablecolumns input.txt {width:90px; border:1px solid #ccc}
            #tablecolumns input.txt2 {width:40px;  border:1px solid #ccc}
            #tablecolumns input.chk { background:#fff}
            #tablecolumns select {border:1px solid #ccc}
            #tablecolumns tr.readonly, #tablecolumns tr.readonly input, #tablecolumns tr.readonly select, #tablecolumns tr.readonly input.chk { background:#f4f4f4; color:#666}
            #tablecolumns tr.h { background:#ffffaa}
            #tablecolumns tr.h input.chk{background:#ffffaa}
            #tablecolumns tr.cur { background:#c2ffbf}
            #tablecolumns tr.cur input.chk{background:#c2ffbf}
            #tablecolumns td.trYline, #tablecolumns th.trYline {border-right:1px solid #000}
            #sortPannel { position:absolute; display:none; background:#c2ffbf; border:1px solid #999; text-align:left; padding:2px}
            #sortPannel button { display:block; height:24px; width:90px; font-size:11px; margin:2px 0; clear:both; color:green}
            #sortPannel h5 { font-size:12px; margin:0 0 4px 0;  padding:2px; font-weight:normal; border-bottom:1px solid #ddd; color:green}
            #sortPannel h5 a { float:right; color:green}
        </style>
    </xsl:template>

    <xsl:template name="js">
        <script type="text/javascript">
            <xsl:comment>
                function moveUp(){
                var cur = $("#tablecolumns tbody tr.cur");
                cur.after(cur.prev("tr"));
                }
                function moveDown(){
                var cur = $("#tablecolumns tbody tr.cur");
                cur.before(cur.next("tr"));
                }
                function sortColumn(obj){
                $(obj.parentNode.parentNode).addClass("cur");
                $("#sortPannel").css({"left":event.clientX-30,"top":event.clientY-34+parseInt(document.documentElement.scrollTop)}).fadeIn('fast');
                }

                function addColumns(n){
                var maxcolid = parseInt($("#maxcolid").val())+1;
                var samplecode = $("#tablecolumns tr.tr_title").length &gt; 0 ? $("#tablecolumns tr.tr_title").html() : $("#tablecolumns tr.tr_name").html();
                for(var i=0; i&lt;n; i++){                    
                $("#tablecolumns tr.tr_datecreated").before("&lt;tr class='tr_title'&gt;"+samplecode+"&lt;/tr&gt;");
                var newtr = $("#tablecolumns tr.tr_datecreated").prev("tr");
                $(newtr).attr("class","tr_custome");
                var tds = $(newtr).children("td");
                $($(tds[0]).children("input")[0]).attr("name","_colid").val(maxcolid++);
                $($(tds[0]).children("input")[1]).attr("name","_colname").val("");
                $($(tds[1]).children("select")[0]).attr("name","_coltype");
                $($(tds[2]).children("input")[0]).attr("name","_colsize").val("");
                $($(tds[3]).children("input")[0]).attr("name","_colbenull");
                $($(tds[4]).children("input")[0]).attr("name","_coldefault").val("");
                $($(tds[5]).children("select")[0]).attr("name","_colcontrol");
                $($(tds[6]).children("input")[0]).attr("name","_colnamezhcn").val("");
                $($(tds[7]).children("input")[0]).attr("name","_colnameenus").val("");

                $($(tds[8]).children("input")[0]).attr("name","_collist").val(0);
                $($(tds[8]).children("input")[1]).attr("checked",false);

                $($(tds[9]).children("input")[0]).attr("name","_coladd").val(0);
                $($(tds[9]).children("input")[1]).attr("checked",false);

                $($(tds[10]).children("input")[0]).attr("name","_colmodify").val(0);
                $($(tds[10]).children("input")[1]).attr("checked",false);

                $($(tds[11]).children("input")[0]).attr("name","_colview").val(0);
                $($(tds[11]).children("input")[1]).attr("checked",false);
                }
                $("#maxcolid").val(maxcolid-1);
                }

                function checkForm(f){
                var colnames = $("#tablecolumns input[name=colname],#tablecolumns input[name=_colname]");
                var colobj = {};
                for(var i=0; i&lt;colnames.length; i++){
                var colname = $.trim(colnames[i].value);
                if(colname=="")continue;
                colnames[i].value = colname;
                if(!/^[a-z]+[0-9a-z_]*/.test(colname))
                {
                alert(colname+"字段名称必须以字母开头，仅接受a-z0-9");
                colnames[i].focus();
                return false;
                }
                if(colobj[colname]){
                alert("字段名重复！");
                colnames[i].focus();
                return false;
                }
                colobj[colname] = 1;
                }

                /*
                var benull = $("input[name=_colbenull]");
                for(var i=0; i&lt;benull.length; i++){
                if($(benull[i]).val()=="0"){
                if($.trim($("input[name=coldefault]")[i].value)==""){
                alert("字段不能为空");
                $("input[name=coldefault]")[i].focus();
                return false;
                }
                }
                }
                */


                return true;
                }

                function removeColumn(obj){
                if(!confirm("确定要删除此字段吗？数据将不可恢复！"))return;
                $(obj.parentNode.parentNode).remove();
                }
            </xsl:comment>
        </script>
    </xsl:template>
 
    
</xsl:stylesheet>